package main

import (
	"fmt"
	"github.com/elastic/go-elasticsearch/v8"
	"github.com/elastic/go-elasticsearch/v8/esutil"
)

func main() {
	client, err := elasticsearch.NewClient(
		elasticsearch.Config{
			Addresses: []string{"http://192.168.16.109:9200"},
			Username:  "elastic",
			Password:  "56248Qwezxcv",
			//EnableDebugLogger: true,
		},
	)

	if err != nil {
		panic(err)
	}

	result, err := client.Search(
		client.Search.WithIndex("user"),
	)
	if err != nil {
		return
	}
	defer result.Body.Close()
	fmt.Println(result)

	result, err = client.Search(
		client.Search.WithIndex("user"),
		client.Search.WithBody(
			esutil.NewJSONReader(
				map[string]any{
					"query": map[string]any{
						"match_all": map[string]any{},
					},
				},
			),
		),
		client.Search.WithPretty(),
	)
	if err != nil {
		return
	}
	defer result.Body.Close()

	fmt.Println(result)

	//q := elastic.NewMatchQuery("address", "street")
	//result, err := client.Search().Index("user").Query(q).Do(context.Background())
	//if err != nil {
	//	panic(err)
	//}
	//total := result.Hits.TotalHits.Value
	//fmt.Printf("搜索结果数量: %d\n", total)
	//for _, value := range result.Hits.Hits {
	//	account := Account{}
	//	err := json.Unmarshal(value.Source, &account)
	//	if err != nil {
	//		return
	//	}
	//	fmt.Println(account)
	//}
	//
	////// Index a tweet (using JSON serialization)
	////tweet1 := Account{AccountNumber: 15368, FirstName: "czc learn"}
	////put1, err := client.Index().Index("myuser").BodyJson(tweet1).Do(context.Background())
	////if err != nil {
	////	// Handle error
	////	panic(err)
	////}
	////fmt.Printf("Indexed tweet %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
	//
	//_, err = client.CreateIndex("mygoods").BodyString(goodsMapping).Do(context.Background())
	//if err != nil {
	//	panic(err)
	//}

}
